package com.etoak.config;

import com.etoak.common.core.properties.ImageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;

@Configuration
public class WebFluxConfiguration implements WebFluxConfigurer {

    @Autowired
    ImageProperties imageProperties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //mapping  = "/pics/**"
        String mapping = imageProperties.getMapping().endsWith("/") ?
                imageProperties.getMapping() + "**" :
                imageProperties.getMapping() + "/**";

        // location = "file:d:/pro/et2503/"
        String location = "file:" + (imageProperties.getUploadDir().endsWith("/") ?
                imageProperties.getUploadDir() :
                imageProperties.getUploadDir() + "/");

        registry.addResourceHandler(mapping).addResourceLocations(location);
    }
}
